import { add, edit, detail } from "../../api/index.js";
// var app = getApp();
// var QQMapWX = require("../../qqmap-wx-jssdk.min.js");
// var qqmapsdk;
Page({
  data: {
    id: "先生",
    city: "点击选择",
    name: "",
    phone: "",
    cityDetail: "",
    address: "", //当前定位的城市
    nothing: false,
    addressId: "",
  },
  onLoad(options) {
    console.log("编辑", options);
    this.setData({
      addressId: options.id,
    });
    if (options.id) {
      this.getAddressDetail();
    }
  },
  getAddressDetail: async function () {
    const params = {
      id: this.data.addressId,
    };
    const tableName = "address";
    const res = await detail(this.data.addressId, tableName);
    if (res.code === 200) {
      this.setData({
        id: res.data.gender,
        name: res.data.receiptName,
        phone: res.data.phone,
        cityDetail: res.data.address,
      });
      // this.setData({
      //   address: res.data,
      // });
    }
  },
  getAddress: function () {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        if (res.address.length > 10) {
          res.address = res.address.substr(0, 10) + "...";
        }
        that.setData({
          city: res.address,
        });
      },
    });
  },
  chooseId: function (e) {
    var type = e.currentTarget.dataset.id;
    this.setData({
      id: type,
    });
  },
  toEditAddress: function () {
    this.setData({
      pageType: 2,
    });
  },
  noChoose: function () {
    this.setData({
      pageType: 1,
      city: "点击选择",
    });
  },
  setName: function (e) {
    this.setData({
      name: e.detail.value,
    });
  },
  setPhone: function (e) {
    this.setData({
      phone: e.detail.value,
    });
  },
  setCityDetail: function (e) {
    this.setData({
      cityDetail: e.detail.value,
    });
  },
  // saveAddress: function() {
  //   console.log(this.data)
  //   wx.navigateBack({
  //     url: '../address/address',
  //   })
  //   wx.request({
  //     url: '',
  //     data: this.data,
  //     success: function(res) {
  //       console.log(res)
  //     }
  //   })
  // },
  async formSubmit(e) {
    console.log("form发生了submit事件，携带数据为：", e.detail.value);

    if (this.data.addressId) {
      const params = {
        id: this.data.addressId,
        userId: wx.getStorageSync("userInfo").userId,
        receiptName: e.detail.value.name,
        phone: e.detail.value.phone,
        city: e.detail.value.city,
        address: e.detail.value.cityDetail,
        gender: e.detail.value.sex,
      };
      const tableName = "address";
      const res = await edit(params, tableName);
      if (res.code === 200) {
        wx.showToast({
          title: "修改成功",
          icon: "success",
          duration: 2000,
        })
      }
    } else {
      const params = {
        userId: wx.getStorageSync("userInfo").userId,
        receiptName: e.detail.value.name,
        phone: e.detail.value.phone,
        city: e.detail.value.city,
        address: e.detail.value.cityDetail,
        gender: e.detail.value.sex,
      };
      const tableName = "address";
      const res = await add(params, tableName);
      if (res.code === 200) {
        wx.showToast({
          title: "添加成功",
          icon: "success",
          duration: 2000,
        })
      }
    }
  },
  formReset: function () {
    console.log("form发生了reset事件");
  },
});
